package com.union.vehicleassistant.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.union.vehicleassistant.R;
import com.union.vehicleassistant.app.AppAdapter;
import com.union.vehicleassistant.bean.IAdjunct;
import com.union.vehicleassistant.util.FormatEnum;
import com.union.vehicleassistant.util.callback.SimpleCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 附件对话框
 *
 * @author mz
 * @date-time 2024/11/21 15:43
 */
public final class AdjunctDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements BaseAdapter.OnItemClickListener {

        private final AdjunctAdapter mAdapter;

        @Nullable
        private SimpleCallback<IAdjunct> mListener;

        public Builder(Activity activity) {
            super(activity);

            setContentView(R.layout.adjunct_dialog);

            final List<IAdjunct> data = new ArrayList<>();


            mAdapter = new AdjunctAdapter(activity);
            mAdapter.setData(data);
            mAdapter.setOnItemClickListener(this);

            RecyclerView mRecyclerView = findViewById(R.id.rv_share_list);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
            mRecyclerView.setAdapter(mAdapter);

            setOnClickListener(R.id.tv_cancel);
        }

        /**
         * 设置新的数据
         */
        public Builder setNewData(List<IAdjunct> data) {
            mAdapter.setData(data);
            return this;
        }


        /**
         * 设置回调监听器
         */
        public Builder setListener(SimpleCallback<IAdjunct> listener) {
            mListener = listener;
            return this;
        }

        /**
         * {@link BaseAdapter.OnItemClickListener}
         */
        @Override
        public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
            IAdjunct item = mAdapter.getItem(position);
            if (null != mListener)
                mListener.onResult(item);
            dismiss();
        }

        @Override
        public void onClick(View view) {
            super.onClick(view);
            dismiss();
        }
    }

    private static class AdjunctAdapter extends AppAdapter<IAdjunct> {

        private AdjunctAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder();
        }

        private final class ViewHolder extends AppAdapter<?>.ViewHolder {

            private final TextView mTextView;
            private final ImageView ivFileIcon;

            private ViewHolder() {
                super(R.layout.adjunct_item);
                mTextView = findViewById(R.id.tv_file_name);
                ivFileIcon = findViewById(R.id.iv_file_icon);
            }

            @Override
            public void onBindView(int position) {
                IAdjunct bean = getItem(position);
                mTextView.setText(bean.getFileName());
                String extension = FileUtils.getFileExtension(bean.getFileName());
                FormatEnum format = FormatEnum.getFormat(extension);
                ivFileIcon.setImageResource(format.iconResourceId);
            }
        }
    }
}